/**
* 版权所有：福建邮科电信业务部厦门研发中心 
*====================================================
* 文件名称: MyMain.java
* 修订记录：
* No    日期				作者(操作:具体内容)
* 1.    2013-3-27			詹保山(创建:创建文件)
*====================================================
* 类描述：(说明未实现或其它不应生成javadoc的内容)
* 
*/
package com.devframe.gc;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;



import com.devframe.gc.core.ZBSGCConstants;
import com.devframe.gc.core.ZBSGCContainer;
import com.devframe.gc.freemarker.directive.ZBSGCPageDirective;
import com.devframe.gc.handle.BeanFieldHandleFacade;
import com.devframe.gc.meta.ZBSGCBeanInfoMeta;
import com.devframe.gc.meta.ZBSGCFileInfoMeta;
import com.devframe.gc.meta.ZBSGCMethodInfoMeta;
import com.devframe.gc.util.ZBSGCFileUtil;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class ZBSGCFactory {
	private static Configuration   configuration;
	
	private static void generateFile(String ftlTemplate,Map<String,Object> root,String savePath){
		try {
			Template temp;
			Writer out=null;
			temp = configuration.getTemplate(ftlTemplate);
			out= new StringWriter();
			//设置不执行数值格式转化-解决逗号问题
			temp.setNumberFormat("#.##");
			temp.process(root, out);
			out.flush();
			ZBSGCFileUtil.createNewFile(savePath,out.toString());
			out=null;
		}catch(Exception e){
			
		}
	}
	
	/**
	 * @param args
	 * @author:詹保山
	 * @throws IOException 
	 */
	public static void gc(Class targetClass)  {
		//解析系统参数
		ResourceBundle rb=ResourceBundle.getBundle("zbsgcConfig");
		Enumeration<String> es=rb.getKeys();
		while(es.hasMoreElements()){
			String key=es.nextElement();
			ZBSGCContainer.put(key, rb.getString(key));
		}
		Map<String,Object> root=new HashMap<String,Object>();
	
		ZBSGCBeanInfoMeta beanMeta= BeanFieldHandleFacade.parseClazzFields(targetClass,root);
		ZBSGCMethodInfoMeta actionMeta=new ZBSGCMethodInfoMeta(beanMeta);
		ZBSGCFileInfoMeta fileInfoMeta=new ZBSGCFileInfoMeta(beanMeta);
		root.put("beanMeta", beanMeta);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		root.put("date", sdf.format(new Date()));
		root.put("methodMeta", actionMeta);
		root.put("fileMeta", fileInfoMeta);
		
	   configuration = null;
		if(configuration==null){
			configuration=new Configuration();
			configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			configuration.setObjectWrapper(new DefaultObjectWrapper());
			configuration.setClassicCompatible(true);
			configuration.setEncoding(Locale.getDefault(), "UTF-8");
			try {
				configuration.setDirectoryForTemplateLoading(new File(ZBSGCContainer.get(ZBSGCConstants.TEMPLATE_DIR_KEY)));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		//全局性操作
			root.put("zbsgcFieldContent", new ZBSGCPageDirective());
			root.put("fieldList", beanMeta.getBeanFieldList());
			System.out.println("DAO接口文件路径："+fileInfoMeta.getDaoPath());
			generateFile("Dao.ftl",root,fileInfoMeta.getDaoPath());
			
			
			System.out.println("DAO实现类文件路径："+fileInfoMeta.getDaoImplPath());
			generateFile("DaoImpl.ftl",root,fileInfoMeta.getDaoImplPath());
			

			System.out.println("Service接口文件路径："+fileInfoMeta.getServicePath());
			generateFile("Service.ftl",root,fileInfoMeta.getServicePath());
			
			
			System.out.println("ServiceImpl类型文件路径："+fileInfoMeta.getServiceImplPath());
			generateFile("ServiceImpl.ftl",root,fileInfoMeta.getServiceImplPath());
			

			System.out.println("Action文件路径："+fileInfoMeta.getActionPath());
			generateFile("Action.ftl",root,fileInfoMeta.getActionPath());
			
			
			System.out.println("新增界面文件路径："+fileInfoMeta.getCreatePagePath());
			generateFile("CreatePage.ftl",root,fileInfoMeta.getCreatePagePath());
			
			
			System.out.println("修改界面文件路径："+fileInfoMeta.getUpdatePagePath());
			generateFile("UpdatePage.ftl",root,fileInfoMeta.getUpdatePagePath());
			
			
			System.out.println("查看界面文件路径："+fileInfoMeta.getViewPagePath());
			generateFile("ViewPage.ftl",root,fileInfoMeta.getViewPagePath());
			
			
			System.out.println("查询界面文件路径："+fileInfoMeta.getListPagePath());
			generateFile("ListPage.ftl",root,fileInfoMeta.getListPagePath());
			

			


			
		}
		
		

	}

}
